package com.zhiyizhen.handheld.component.pharmacy.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zhiyizhen.handheld.common.DataCache;
import com.zhiyizhen.handheld.common.ScreenUtil;
import com.zhiyizhen.handheld.component.main.bean.EnumFieldBean;
import com.zhiyizhen.handheld.component.pharmacy.bean.DetailListBean;
import com.zhiyizhen.handheld.component.pharmacy.bean.RxDetailListBean;
import com.zhiyizhen.handheld.component.pharmacy.bean.RxListBean;
import com.zhiyizhen.handheld.component.pharmacy.view.OtherDetailLayout;
import com.zhiyizhen.handheld.component.pharmacy.view.PharmacyChengyaoUnitLayout;
import com.zhiyizhen.handheld.databinding.ItemRegimenChengyaoBinding;
import com.zhiyizhen.handheld.databinding.ItemRegimenOtherBinding;
import com.zhiyizhen.handheld.databinding.ItemRegimenShuyeBinding;

import java.util.List;

public class RegimenDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    // 中西医成药
    private static final int TYPE_CHENGYAO = 1;
    // 输液
    private static final int TYPE_SHUYE = 2;
    // 其他
    private static final int TYPE_OTHER = 3;

    private final Context context;
    private List<RxListBean> mDatas;

    public RegimenDetailAdapter(Context context) {
        this.context = context;
    }

    public void setDatas(List<RxListBean> mDatas) {
        this.mDatas = mDatas;
    }

    public class ChengyaoViewHolder extends RecyclerView.ViewHolder {
        private final ItemRegimenChengyaoBinding binding;

        public ChengyaoViewHolder(ItemRegimenChengyaoBinding viewBinding) {
            super(viewBinding.getRoot());
            binding = viewBinding;
        }

        public void bindData(RxListBean data) {
            binding.rxTypeTv.setText(data.getRxName());
            binding.storageNameTv.setText(data.getStorageName());
            if (TextUtils.equals(data.getDispenseStatus(), "3")) {
                binding.checkbox.setEnabled(false);
            } else {
                binding.checkbox.setEnabled(true);
            }
            binding.checkbox.setChecked(TextUtils.equals(data.getDispenseStatus(), "1"));
            binding.checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> data.setDispenseStatus(isChecked ? "1" : "0"));

            List<RxDetailListBean> detailList = data.getDetailList();
            if (detailList != null && !detailList.isEmpty()) {
                for (int i = 0; i < detailList.size(); i++) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    binding.chengyaoLayout.addView(new PharmacyChengyaoUnitLayout(context, i + 1, data.getDetailList().get(i)), layoutParams);
                }
                if (detailList.get(0).getTotalOutQty() - detailList.get(0).getDispenseQty() <= 0) {
                    binding.checkbox.setEnabled(false);
                } else {
                    binding.checkbox.setEnabled(true);
                }
            }
            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        }
    }

    public class ShuyeViewHolder extends RecyclerView.ViewHolder {

        private final ItemRegimenShuyeBinding binding;

        public ShuyeViewHolder(ItemRegimenShuyeBinding viewBinding) {
            super(viewBinding.getRoot());
            binding = viewBinding;
        }

        public void bindData(RxListBean data) {
            binding.rxTypeTv.setText(data.getRxName());
            binding.storageNameTv.setText(data.getStorageName());
            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            binding.recyclerView.setLayoutManager(layoutManager);
            RegimenShuyeGroupAdapter adapter = new RegimenShuyeGroupAdapter(context);
            binding.recyclerView.setAdapter(adapter);
            adapter.setData(data.getGroupList());
            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

            adapter.setOnCheckedChangeListener((data1, isChecked) -> data.setDispenseStatus(isChecked ? "1" : "0"));
        }
    }

    public class OtherViewHolder extends RecyclerView.ViewHolder {
        private final ItemRegimenOtherBinding binding;

        public OtherViewHolder(ItemRegimenOtherBinding viewBinding) {
            super(viewBinding.getRoot());
            binding = viewBinding;
        }

        public void bindData(RxListBean data) {
            binding.rxTypeTv.setText(data.getRxName());
            if (TextUtils.equals(data.getDispenseStatus(), "3") || (data.getTcmInfo().getTotalDoses() - data.getTcmInfo().getDispenseQty()) <= 0) {
                binding.checkbox.setEnabled(false);
            } else {
                binding.checkbox.setEnabled(true);
            }
            binding.checkbox.setChecked(TextUtils.equals(data.getDispenseStatus(), "1"));
            binding.checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                data.setDispenseStatus(isChecked ? "1" : "0");
            });

            binding.storageNameTv.setText(data.getStorageName());
            if (data.getTcmInfo() != null) {
                binding.totalCountTv.setText(data.getTcmInfo().getTotalDoses() + "剂");
                binding.gavedCountTv.setText(data.getTcmInfo().getDispenseQty() + "剂");
                binding.thisTimeCountTv.setText((data.getTcmInfo().getTotalDoses() - data.getTcmInfo().getDispenseQty()) + "剂");
                binding.frequencyCodeTextTv.setText(data.getTcmInfo().getFrequencyCodeText());
                binding.remarkTv.setText(data.getTcmInfo().getRemark());
                binding.typeTv.setText(data.getTcmInfo().getUsageCodeText());

                String consumption;
                EnumFieldBean enumFieldBean;
                if (DataCache.getEnumsBean().getEnums().getTcmSingleDosageEnum() != null) {
                    for (int i = 0; i < DataCache.getEnumsBean().getEnums().getTcmSingleDosageEnum().size(); i++) {
                        enumFieldBean = DataCache.getEnumsBean().getEnums().getTcmSingleDosageEnum().get(i);
                        if (enumFieldBean.getValue().equals(data.getTcmInfo().getSingleDosage())) {
                            consumption = enumFieldBean.getText();
                            binding.consumptionTv.setText(consumption);
                            break;
                        }
                    }
                }
                List<DetailListBean> detailList = data.getTcmInfo().getDetailList();
                DetailListBean detailListBean;
                if (detailList != null && !detailList.isEmpty()) {
                    for (int i = 0; i < detailList.size(); i++) {
                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        layoutParams.leftMargin = ScreenUtil.dp2px(16);
                        layoutParams.rightMargin = ScreenUtil.dp2px(16);

                        detailListBean = detailList.get(i);
                        binding.detailContentLayout.addView(new OtherDetailLayout(context, detailListBean.getDrugName(), String.valueOf(detailListBean.getDosage()), detailListBean.getTotalOutQtyUnitText()), layoutParams);
                    }
                }
            }
            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (TextUtils.equals(mDatas.get(position).getRxType(), "1")) {
            return TYPE_CHENGYAO;
        }
        if (TextUtils.equals(mDatas.get(position).getRxType(), "2")) {
            return TYPE_SHUYE;
        }
        return TYPE_OTHER;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == TYPE_CHENGYAO) {
            return new ChengyaoViewHolder(ItemRegimenChengyaoBinding.inflate(LayoutInflater.from(viewGroup.getContext())));
        } else if (viewType == TYPE_SHUYE) {
            return new ShuyeViewHolder(ItemRegimenShuyeBinding.inflate(LayoutInflater.from(viewGroup.getContext())));
        } else {
            return new OtherViewHolder(ItemRegimenOtherBinding.inflate(LayoutInflater.from(viewGroup.getContext())));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, final int position) {
        if (viewHolder instanceof ChengyaoViewHolder holder) {
            holder.bindData(mDatas.get(position));
        } else if (viewHolder instanceof ShuyeViewHolder shuyeViewHolder) {
            shuyeViewHolder.bindData(mDatas.get(position));
        } else {
            OtherViewHolder otherViewHolder = (OtherViewHolder) viewHolder;
            otherViewHolder.bindData(mDatas.get(position));
        }
    }

}
